import axios from "axios";
import Loading from "../../components/loading/index";




//配置 axios 实例
const axiosInstance = axios.create({
    baseURL: "https://api.it120.cc/small4",
    timeout: 10000
})

let Loadig = null;
//请求发送前，弹出loading组件
// 添加请求拦截器
axiosInstance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    //弹起loading
    // console.log(Loading);
    if(!Loadig){
        Loadig = Loading.serve()
    }
    
    return config;
}, function (error) {
    // 对请求错误做些什么
    //提示用户错误信息
    return Promise.reject(error);
});

//请求结束后，收起loading组件
// 添加响应拦截器
axiosInstance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    Loadig.close();
    return response;
}, function (error) {
    // 对响应错误做点什么
    Loadig.close();
    return Promise.reject(error);
});

export function request(method, url, params) {
    switch (method) {
        case "GET":
           return GET(url, params);
            break;
        case "POST":
            return POST(url, params);
            break;
        case "UPLOAD":
            return UPLOAD(url, params);
            break;
        case "DOWNLOAD":
            return DOWNLOAD(url, params)
            break;
    }
}

// 提供不同请求
function GET(url, params) {
    return axiosInstance.get(url, params)
}

function POST(url, params) {
    return axiosInstance.post(url, params)
}

// function UPLOAD(url,params){
//    return axiosInstance.get(url,params)
// }

// function DOWNLOAD(url,params){
//    return axiosInstance.get(url,params)
// }